<template>
  <transition name="down">
    <div
      class="min-w-[420px] fixed top-[20px] left-[50%] translate-x-[-50%] z-50 flex rounded-sm items-center px-3 py-1.5 border cursor-pointer"
      :class="styles[type].containerClass"
      v-show="isVisable"
    >
      <m-svg-icon
        :name="styles[type].icon"
        :fillClass="styles[type].fillClass"
        class="h-1.5 w-1.5 mr-1.5"
      ></m-svg-icon>
      <span class="text-sm" :class="styles[type].textClass">{{ content }}</span>
    </div>
  </transition>
</template>

<script>
import { ref } from '@vue/reactivity'
import mSvgIcon from '@/libs/svg-icon/index.vue'
import { onMounted } from '@vue/runtime-core'

const SUCCESS = 'success'
const WARM = 'warn'
const ERROR = 'error'
const typeEnum = [SUCCESS, WARM, ERROR]
</script>

<script setup>
const props = defineProps({
  type: {
    type: String,
    required: true,
    validator(val) {
      const result = typeEnum.includes(val)
      if (!result) {
        throw new Error(`您必须选择${typeEnum.join('、')}类型中的一个`)
      }
      return result
    }
  },
  content: {
    type: String,
    required: true
  },
  // 展示时长
  duration: {
    type: Number
  },
  // 关闭的回调
  destory: {
    type: Function
  }
})

// 是否可见
const isVisable = ref(false)

// 动画时长
const animDuration = '0.5s'

//为了控制动画展示需要在mounted之后去改变isVisable
onMounted(() => {
  isVisable.value = true
  setTimeout(() => {
    isVisable.value = false
    setTimeout(() => {
      props.destory()
    }, parseInt(animDuration.replace('0.', '').replace('s', '') * 100))
  }, props.duration)
})

// 样式表数据
const styles = {
  // 警告
  warn: {
    // 图标
    icon: 'warn',
    // 图标颜色
    fillClass: 'fill-warn-300',
    // 文字颜色
    textClass: 'text-warn-300',
    containerClass:
      'bg-warn-100 border-warn-200 hover:shadow-lg hover:shadow-warn-100'
  },
  // 错误
  error: {
    // 图标
    icon: 'error',
    // 图标颜色
    fillClass: 'fill-error-300',
    // 文字颜色
    textClass: 'text-error-300',
    containerClass:
      'bg-error-100 border-error-200 hover:shadow-lg hover:shadow-error-100'
  },
  // 成功
  success: {
    // 图标
    icon: 'success',
    // 图标颜色
    fillClass: 'fill-success-300',
    // 文字颜色
    textClass: 'text-success-300',
    containerClass:
      'bg-success-100 border-success-200 hover:shadow-lg hover:shadow-success-100'
  }
}
</script>
<style lang="scss" scoped>
.down-enter-active,
.down-leave-active {
  transition: all v-bind(animDuration);
}
.down-enter-from,
.down-leave-to {
  opacity: 0;
  transform: translate3d(-50%, -100px, 0);
}
</style>
